﻿''' <summary>
''' 多段线数据表
''' 映射表: poly_line_data 
''' </summary>
Public Class PolyLineDataMapper
    Private db As IMySqlService = New MySqlServiceImpl

    Public Function [Select](ByRef ent As PolyLineDataEntity) As DataTable
        Dim sql As String = <sql>
                                select * from poly_line_data where 1 = 1
                            </sql>
        If ent.id > 0 Then sql.Append($" and id = {ent.id}")
        If Not ent.name Is Nothing AndAlso ent.name <> "" Then sql.Append($" and `name` like '%{ent.name}'%")
        If Not ent.remark Is Nothing AndAlso ent.remark <> "" Then sql.Append($" and `remark` like '%{ent.remark}'%")
        Return db.select(sql)
    End Function


    Public Function [Select_ById](ByVal id As Long) As DataTable
        Dim sql As String = <sql>
                                select * from poly_line_data where line_id = {0}
                            </sql>
        Return db.select(String.Format(sql, id))

    End Function

    Public Function [Info](ByRef id As Long) As PolyLineDataEntity
        Dim dt As DataTable = Select_ById(id)
        If dt.Rows.Count < 1 Then
            Return Nothing
        Else
            Return New PolyLineDataEntity(dt.Rows(0))
        End If
    End Function

    Public Function [List](ByRef ent As PolyLineDataEntity) As List(Of PolyLineDataEntity)
        Dim dt As DataTable = [Select](ent)

        Dim lt As New List(Of PolyLineDataEntity)
        For Each dr As DataRow In dt.Rows
            lt.Add(New PolyLineDataEntity(dr))
        Next

        Return lt
    End Function

    Public Function [Insert](ByRef ent As PolyLineDataEntity) As Integer
        Dim maxid As Long = getMaxId()
        Dim sql As New Text.StringBuilder("insert into poly_line_data set ")
        sql.Append($" `id` = '{maxid + 1}'")
        sql.Append($", `name` = '{ent.name}'")
        sql.Append($", `remark` = '{ent.remark}'")
        sql.Append($", `coordinates` = '{String.Join(",", ent.coordinates)}'")
        sql.Append($", `bluges` =  '{String.Join(",", ent.bluges)}'")
        Return db.insert(sql.ToString)
    End Function

    Public Function [Insert](ByRef lt As List(Of PolyLineDataEntity)) As Integer
        Dim maxid As Long = getMaxId()
        Dim sql As New Text.StringBuilder("insert into poly_line_data (`id`,`name`,`remark`,`coordinates`,`bluges`) values ")
        Dim values As New List(Of String)
        Dim val As Text.StringBuilder
        For Each ent As PolyLineDataEntity In lt
            maxid += 1
            val = New Text.StringBuilder("(")
            With val
                .Append($" '{maxid}'")
                .Append($",'{ent.name}'")
                .Append($",'{ent.remark}'")
                .Append($",'{String.Join(",", ent.coordinates)}'")
                .Append($",'{String.Join(",", ent.bluges)}'")
                .Append(")")
            End With
            values.Add(val.ToString)
        Next ent
        Return db.insert(sql.Append(String.Join(",", values)).ToString)
    End Function

    Public Function getMaxId() As Long
        Dim sql As String = "select max(`id`) from poly_line_data"
        Dim id As Object = db.getValue(sql)
        If IsDBNull(id) Then Return 0L
        Return id
    End Function
End Class
